package client;

import android.app.Activity;
import android.content.Intent;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import androidx.lifecycle.LifecycleOwner;

import java.io.File;
import java.util.ArrayList;

import client.activity.BGAPhotoPickerActivity;
import client.activity.BGAPhotoPickerPreviewActivity;
import client.activity.BGATakePhotoActivity;

/**
 * 项目: com.qrcode.zxing.utils
 * 创建人: LuoFG
 * 创建时间: 2021/7/9  14:36
 * 描述:
 */
public class PhotoClient {

    private static class InstanceHolder {
        private static PhotoClient instance = new PhotoClient();
    }

    private static PhotoClient getInstance() {
        return InstanceHolder.instance;
    }

    public static PhotoClient getClient() {
        return getInstance();
    }

    /***配置资源-Start***/
    //是否是多选 默认单选
    private boolean isMultiSelectPhoto = false;
    //多选的选择数量
    private int mMultiSelectNum = 3;
    //是否有拍照功能
    private boolean isTakePhoto = false;
    //是否支持裁剪
    private boolean isCropPhoto = false;
    //拍照后照片的存放目录，改成你自己拍照后要存放照片的目录。如果不传递该参数的话则不开启图库里的拍照功能
    private String strTakePhotoPath = "";
    //已选图片的数组（根据需求，一般为空）
    private ArrayList<String> selectPhotoList;
    //当前选择的图片索引
    private int mCurrentPosition = 0;
    /***配置资源-End***/
    //传入具体包名类  作为区分
    private String PhotoResultTag = "";

    //默认Bar文字白色
    private int barTextColor = R.color.White;
    //默认Bar背景白色
    private int barBgColor = R.color.Blue;
    //标题栏颜色是否沉浸到状态栏
    private boolean isToOsBar = true;
    //是否显示更多 仅图片预览使用
    private boolean isShowMore = false;
    //是否显示 仅图片预览使用
    private boolean isShowTitle = false;
    //是否启用前置摄像头
    private boolean isFrontCamera = false;
    //是否显示 底部的数字
    private boolean isShowBtmNum = true;
    //是否一直显示标题栏
    private boolean isAlwaysShowTitle = false;
    //设置成为默认属性
    public PhotoClient setDefault() {
        //默认Bar文字白色
        barTextColor = R.color.White;
        //默认Bar背景白色
        barBgColor = R.color.Blue;
        //标题栏颜色是否沉浸到状态栏
        isToOsBar=true;
        //是否是多选 默认单选
        isMultiSelectPhoto = false;
        //多选的选择数量
        mMultiSelectNum = 3;
        //是否有拍照功能
        isTakePhoto = false;
        //是否支持裁剪
        isCropPhoto = false;
        //拍照后照片的存放目录，改成你自己拍照后要存放照片的目录。如果不传递该参数的话则不开启图库里的拍照功能
        strTakePhotoPath = "";
        //已选图片的数组（根据需求，一般为空）
        selectPhotoList = null;
        //当前选择的图片索引
        mCurrentPosition = 0;
        //传入具体包名类  作为区分
        PhotoResultTag = "";
        //是否显示更多 仅图片预览使用
        isShowMore=false;
        //是否启用前置摄像头
        isFrontCamera=false;
        //是否显示 底部的数字
        isShowBtmNum = false;
        return this;
    }


    /**
     * @param activity 启动图片选择
     *                 调用本方法前需要检查存储权限和相机权限
     */
    public void startPhotoPicker(Activity activity) {
        File takePhotoDir = null;
        if (isTakePhoto) {
            if(TextUtils.isEmpty(strTakePhotoPath)) {
                takePhotoDir = new File(Environment.getExternalStorageDirectory(), "BGAPhotoPickerTakePhoto");
            }else{
                takePhotoDir = new File(strTakePhotoPath);
            }
        }

        //如果开启剪裁默认是单选模式
        if(isCropPhoto){
            Intent photoPickerIntent = new BGAPhotoPickerActivity.IntentBuilder(activity)
                    .cameraFileDir(isTakePhoto ? takePhotoDir : null) // 拍照后照片的存放目录，改成你自己拍照后要存放照片的目录。如果不传递该参数的话则不开启图库里的拍照功能
                    .maxChooseCount(1) // 图片选择张数的最大值
                    .selectedPhotos(null) // 当前已选中的图片路径集合
                    .pauseOnScroll(false) // 滚动列表时是否暂停加载图片
                    .build();
            activity.startActivity(photoPickerIntent);
        }else {
            Intent photoPickerIntent = new BGAPhotoPickerActivity.IntentBuilder(activity)
                    .cameraFileDir(isTakePhoto ? takePhotoDir : null) // 拍照后照片的存放目录，改成你自己拍照后要存放照片的目录。如果不传递该参数的话则不开启图库里的拍照功能
                    .maxChooseCount(isMultiSelectPhoto ? 1 : mMultiSelectNum) // 图片选择张数的最大值
                    .selectedPhotos(null) // 当前已选中的图片路径集合
                    .pauseOnScroll(false) // 滚动列表时是否暂停加载图片
                    .build();
            activity.startActivity(photoPickerIntent);
        }

    }


    /**
     * @param activity 启动图片预览  图片预览不需要返回
     *                 调用本方法前需要检查存储权限
     */
    public void startPhotoPreview(Activity activity) {
        if (selectPhotoList == null || selectPhotoList.size() == 0) {
            Log.e("未选择图片", "无法启动图片预览");
            return;
        }

        Intent photoPickerPreviewIntent = new BGAPhotoPickerPreviewActivity.IntentBuilder(activity)
                .previewPhotos(selectPhotoList) // 当前预览的图片路径集合
                .selectedPhotos(selectPhotoList) // 当前已选中的图片路径集合
                .maxChooseCount(selectPhotoList.size()) // 图片选择张数的最大值
                .currentPosition(mCurrentPosition) // 当前预览图片的索引
                .isFromTakePhoto(false) // 是否是拍完照后跳转过来
                .build();
        activity.startActivity(photoPickerPreviewIntent);
    }

    public void takePhoto(Activity activity) {
        try {
            File takePhotoDir;
            if(TextUtils.isEmpty(strTakePhotoPath)) {
                takePhotoDir = new File(Environment.getExternalStorageDirectory(), "PhotoPickerTakePhoto");
            }else{
                takePhotoDir = new File(strTakePhotoPath);
            }
            Intent photoPickerIntent = new BGATakePhotoActivity.IntentBuilder(activity)
                    .cameraFileDir(takePhotoDir) // 拍照后照片的存放目录，改成你自己拍照后要存放照片的目录。如果不传递该参数的话则不开启图库里的拍照功能
                    .crop(isCropPhoto)
                    .build();
            activity.startActivity(photoPickerIntent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public interface PhotoResult{
        void back(PhotoSelectResultBean result);
    }

    public interface PhotoCancel{
        void back();
    }

    public PhotoClient takePhotoResult(Activity activity, PhotoResult photoResult){
        BGATakePhotoActivity.photoResultData.observe((LifecycleOwner) activity, photoSelectResultBean->{
            //Log.e("拍照返回", photoSelectResultBean.getTag() + "       " + photoSelectResultBean.getResult().toString());
            photoResult.back(photoSelectResultBean);
            BGATakePhotoActivity.photoResultData.removeObservers((LifecycleOwner) activity);
        });
        return this;
    }

    public PhotoClient takePhotoCancel(Activity activity, PhotoCancel photoCancel){
        BGATakePhotoActivity.photoResultCancel.observe((LifecycleOwner) activity, isCancel->{
            photoCancel.back();
            BGATakePhotoActivity.photoResultCancel.removeObservers((LifecycleOwner) activity);
        });
        return this;
    }

    public PhotoClient selectPhotoResult(Activity activity, PhotoResult photoResult){
        BGAPhotoPickerActivity.photoResultData.observe((LifecycleOwner) activity, photoSelectResultBean->{
            //Log.e("选择图片返回", photoSelectResultBean.getTag() + "       " + photoSelectResultBean.getResult().toString());
            photoResult.back(photoSelectResultBean);
            BGATakePhotoActivity.photoResultData.removeObservers((LifecycleOwner) activity);
        });
        return this;
    }

   /* public void selectPhotoCancel(Activity activity, PhotoCancel photoCancel){
        BGAPhotoPickerActivity.photoResultCancel.observe((LifecycleOwner) activity, isCancel->{
            photoCancel.back();
        });
    }*/


    public interface MoreClickListener{
        void click(PhotoMoreResultBean photoMoreResultBean);
    }

    private MoreClickListener moreClickListener;

    public void runMoreClickLister(PhotoMoreResultBean photoMoreResultBean){
        if(moreClickListener!=null){
            moreClickListener.click(photoMoreResultBean);
        }
    }

    public void addMoreClickListener(MoreClickListener moreClickListener) {
        this.moreClickListener = moreClickListener;
    }

    public String getPhotoResultTag() {
        return PhotoResultTag;
    }

    public PhotoClient setPhotoResultTag(String photoResultTag) {
        PhotoResultTag = photoResultTag;
        return this;
    }

    public int getBarTextColor() {
        return barTextColor;
    }

    public PhotoClient setBarTextColor(int barTextColor) {
        this.barTextColor = barTextColor;
        return this;
    }

    public int getBarBgColor() {
        return barBgColor;
    }

    public boolean isFrontCamera() {
        return isFrontCamera;
    }

    public PhotoClient setFrontCamera(boolean frontCamera) {
        isFrontCamera = frontCamera;
        return this;
    }

    public PhotoClient setBarBgColor(int barBgColor) {
        this.barBgColor = barBgColor;
        return this;
    }

    public PhotoClient setToOsBar(boolean toOsBar){
        isToOsBar=toOsBar;
        return this;
    }

    public boolean isToOsBar() {
        return isToOsBar;
    }


    public PhotoClient setMultiSelectPhoto(boolean multiSelectPhoto) {
        isMultiSelectPhoto = multiSelectPhoto;
        return this;
    }

    public PhotoClient setmMultiSelectNum(int mMultiSelectNum) {
        this.mMultiSelectNum = mMultiSelectNum;
        return this;
    }

    public PhotoClient setShowMore(boolean showMore) {
        isShowMore = showMore;
        return this;
    }

    public boolean isShowMore() {
        return isShowMore;
    }

    public boolean isAlwaysShowTitle() {
        return isAlwaysShowTitle;
    }

    public PhotoClient setAlwaysShowTitle(boolean alwaysShowTitle) {
        isAlwaysShowTitle = alwaysShowTitle;
        return this;
    }

    public PhotoClient setTakePhoto(boolean takePhoto) {
        isTakePhoto = takePhoto;
        return this;
    }

    public PhotoClient setStrTakePhotoPath(String strTakePhotoPath) {
        this.strTakePhotoPath = strTakePhotoPath;
        return this;
    }

    public PhotoClient setSelectPhotoList(ArrayList<String> selectPhotoList) {
        this.selectPhotoList = selectPhotoList;
        return this;
    }

    public PhotoClient setmCurrentPosition(int mCurrentPosition) {
        this.mCurrentPosition = mCurrentPosition;
        return this;
    }

    public PhotoClient setCropPhoto(boolean cropPhoto) {
        isCropPhoto = cropPhoto;
        return this;
    }

    public boolean isCropPhoto() {
        return isCropPhoto;
    }

    public boolean isShowBtmNum() {
        return isShowBtmNum;
    }

    public PhotoClient setShowBtmNum(boolean showBtmNum) {
        isShowBtmNum = showBtmNum;
        return this;
    }
}
